[#import "/framework/JGP.htm"  as JGP]
<!--
项目   jgp-cloud-parent
作者   LOUFEI [Gavin]
时间   2019-11-01
-->
[@JGP.head "Title"/]
[@JGP.body]
    <jgp-div _col="['300','*']" _cunit="px" aux="true">
        <jgp-panel slot="1-1" _row="['40','*']" _runit="px">
            <jgp-tree2 _id="menuTree" _url="/sys/menu-api-ctl/query-tree-lazy"
                       _extend="true"
                       _onclick="doClickNodeFn"
                       _show_root="true"
                       _lazy="true"
                    [#--_tools="[{'tip':'移动','icon':'exchange','fn':'moveTo'}]"--]
                       _check="true" _root="系统菜单" _single="true"></jgp-tree2>
        </jgp-panel>
        <jgp-div slot="1-2" _row="['30','80','*']" _runit="px">
            <div slot="1-1">
                <jgp-btn _fn="createMenu">新建菜单</jgp-btn>
                <jgp-btn _fn="deleteInBatch" _color="caution">批量删除</jgp-btn>
                <jgp-btn _fn="refreshMenuCache">刷新缓存</jgp-btn>
            </div>
            <jgp-query slot="2-1" _id="menuGridQuery" _action="/sys/menu-api-ctl/query" _target="menuGrid">
                <jgp-form-group _title="搜索菜单">
                    <jgp-text _col="3" _name="title" _placeholder="标题"></jgp-text>
                    <jgp-text _col="3" _name="code" _placeholder="编码"></jgp-text>
                    <jgp-drop _col="3" _name="enabled" _placeholder="是否可用"
                              _list="[{'label':'可用','value':'true'},{'label':'不可用','value':'false'}]"></jgp-drop>
                </jgp-form-group>
            </jgp-query>
            <jgp-grid slot="3-1" _id="menuGrid" _tools_name="true"
                      _record_tools="[{'tip':'编辑','fn':'editMenu','icon':'edit'},{'tip':'删除','fn':'deleteOne','icon':'trash-o'}]"></jgp-grid>
        </jgp-div>

    </jgp-div>
[/@JGP.body]

[@JGP.foot]
    <script>
        function createMenu() {
            var selected = JGP.tree2('menuTree').getSelected();
            var parentId = -1;
            if (selected.length !== 0) {
                parentId = selected[0].id
            }

            var dialog = {
                'title': '添加菜单项',
                'width': '500px',
                'height': '470px',
                'url': '/sys/menu-ctl/edit-menu-item?parentId=' + parentId,
                'callback': function (result) {
                    location.reload();
                }
            };
            JGP.$dialog(dialog);
        }

        function editMenu(args) {
            var dialog = {
                'title': '修改菜单项:' + args.back.record.title,
                'width': '500px',
                'height': '470px',
                'url': '/sys/menu-ctl/edit-menu-item?id=' + args.back.record.id,
                'callback': function (result) {
                    if (result) {
                        location.reload();
                    }
                }
            };
            JGP.$dialog(dialog);
        }

        function doClickNodeFn(args) {
            JGP.query('menuGridQuery').field('parentId', args.back.key)
        }


        function deleteInBatch() {
            var selected = JGP.grid('menuGrid').getSelected();
            if (selected.length === 0) return;
            JGP.$confirm("是否确定删除？", function () {
                var ids = [];
                for (var i = 0; i < selected.length; i++) {
                    ids.push(selected[i].id);
                }
                JGP.$postJson("/sys/menu-api-ctl/remove-menu", ids, function (result) {
                    if (result.flag) {
                        JGP.$alert("删除成功");
                        JGP.query('menuGridQuery').search();
                    }
                })
            });
        }

        function deleteOne(args) {
            JGP.$confirm("是否确定删除？", function () {
                var ids = [args.back.record.id];

                JGP.$postJson("/sys/menu-api-ctl/remove-menu", ids, function (result) {
                    if (result.flag) {
                        JGP.$alert("删除成功");
                        JGP.query('menuGridQuery').search();
                    }
                })
            });
        }

        function refreshMenuCache() {
            JGP.$post("/sys/menu-api-ctl/refresh-cache", {}, function (result) {
                if (result.flag) {
                    JGP.$alert("刷新成功成功");
                }
            })
        }
    </script>
[/@JGP.foot]